package cn.mw.cmdb.viewProcessor;

import cn.mw.cmdb.entity.InputDataAuth;
import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.processor.TableDataProcessorContext;
import cn.mw.cmdb.view.StaticsViewInstance;
import cn.mw.cmdb.view.ViewConvertContext;
import cn.mw.microMonitorCommon.api.user.MwUserApi;
import cn.mw.microMonitorCommon.entity.userDTO.GroupDTO;
import cn.mw.microMonitorCommon.entity.userDTO.OrgDTO;
import cn.mw.microMonitorCommon.security.dto.MwUser;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;

public class DataAuthViewProcessor implements RelateDataViewProcessor<InstanceInfo, StaticsViewInstance>{
    private ViewProcessorManage viewProcessorManage;

    public DataAuthViewProcessor(ViewProcessorManage viewProcessorManage){
        this.viewProcessorManage = viewProcessorManage;
    }

    @Override
    public void init(InstanceInfo instanceInfo ,StaticsViewInstance staticsViewInstance) {
        List<Long> orgIds = instanceInfo.getOrgIds();
        if(null != orgIds){
            for(Long id : orgIds){
                viewProcessorManage.addFieldIdSet(InputDataAuth.OrgKey ,id.toString());
            }
            staticsViewInstance.setOrgNames(StringUtils.collectionToCommaDelimitedString(orgIds));
        }

        List<Long> groupIds = instanceInfo.getGroups();
        if(null != groupIds) {
            for (Long id : groupIds) {
                viewProcessorManage.addFieldIdSet(InputDataAuth.GroupKey, id.toString());
            }
            staticsViewInstance.setGroupNames(StringUtils.collectionToCommaDelimitedString(groupIds));
        }

        List<Long> userIds = instanceInfo.getUsers();
        if(null != userIds) {
            for (Long id : userIds) {
                viewProcessorManage.addFieldIdSet(InputDataAuth.UserKey, id.toString());
            }
            staticsViewInstance.setUserNames(StringUtils.collectionToCommaDelimitedString(userIds));
        }
    }

    @Override
    public void selectRelateData(TableDataProcessorContext context) {
        MwUserApi mwUserApi = (MwUserApi)context.get(ViewConvertContext.MwuserApiKey);
        Set<String> orgs = viewProcessorManage.getFieldIdSet(InputDataAuth.OrgKey);
        if(null != orgs){
            List<Long> orgIds = orgs.stream().mapToLong(data-> Long.valueOf(data)).boxed().collect(Collectors.toList());
            List<OrgDTO> orgDTOS = mwUserApi.getOrgListByOrgIds(orgIds);
            if(null != orgDTOS){
                for(OrgDTO orgDTO: orgDTOS){
                    viewProcessorManage.addFieldIdMap(InputDataAuth.OrgKey ,orgDTO.getId().toString() ,orgDTO.getOrgName());
                }
            }
        }

        Set<String> groups = viewProcessorManage.getFieldIdSet(InputDataAuth.GroupKey);
        if(null != groups){
            List<Long> groupIds = groups.stream().mapToLong(data-> Long.valueOf(data)).boxed().collect(Collectors.toList());
            List<GroupDTO> groupDTOS = mwUserApi.getGroupListByIds(groupIds);
            if(null != groupDTOS){
                for(GroupDTO groupDTO: groupDTOS){
                    viewProcessorManage.addFieldIdMap(InputDataAuth.GroupKey ,groupDTO.getId().toString() ,groupDTO.getGroupName());
                }
            }
        }

        Set<String> users = viewProcessorManage.getFieldIdSet(InputDataAuth.UserKey);
        if(null != users){
            List<Long> userIds = users.stream().mapToLong(data-> Long.valueOf(data)).boxed().collect(Collectors.toList());
            List<MwUser> mwUsers = mwUserApi.getUsersByUserIds(userIds);
            if(null != mwUsers){
                for(MwUser mwUser: mwUsers){
                    viewProcessorManage.addFieldIdMap(InputDataAuth.UserKey ,mwUser.getId().toString() ,mwUser.getNickName());
                }
            }
        }
    }

    @Override
    public void setRelateData(StaticsViewInstance staticsViewInstance) {
        String orgIdStr = staticsViewInstance.getOrgNames();
        Map<String, String> map = viewProcessorManage.getFieldIdMap(InputDataAuth.OrgKey);
        if(StringUtils.hasText(orgIdStr) && null != map){
            String[] orgIds = StringUtils.commaDelimitedListToStringArray(orgIdStr);
            List<String> orgNames= new ArrayList<>();
            if(null != orgIds){
                for(String id: orgIds){
                    String name = map.get(id);
                    if(StringUtils.hasText(name)){
                        orgNames.add(name);
                    }
                }
            }
            if(orgNames.size() > 0){
                staticsViewInstance.setOrgNames(StringUtils.collectionToCommaDelimitedString(orgNames));
            }
        }

        String groupIdStr = staticsViewInstance.getGroupNames();
        map = viewProcessorManage.getFieldIdMap(InputDataAuth.GroupKey);
        if(StringUtils.hasText(groupIdStr) && null != map){
            String[] groupIds = StringUtils.commaDelimitedListToStringArray(groupIdStr);
            List<String> groupNames= new ArrayList<>();
            if(null != groupIds){
                for(String id: groupIds){
                    String name = map.get(id);
                    if(StringUtils.hasText(name)){
                        groupNames.add(name);
                    }
                }
            }
            if(groupNames.size() > 0){
                staticsViewInstance.setGroupNames(StringUtils.collectionToCommaDelimitedString(groupNames));
            }
        }

        String userIdStr = staticsViewInstance.getUserNames();
        map = viewProcessorManage.getFieldIdMap(InputDataAuth.UserKey);
        if(StringUtils.hasText(userIdStr) && null != map){
            String[] userIds = StringUtils.commaDelimitedListToStringArray(userIdStr);
            List<String> userNames= new ArrayList<>();
            if(null != userIds){
                for(String id: userIds){
                    String name = map.get(id);
                    if(StringUtils.hasText(name)){
                        userNames.add(name);
                    }
                }
            }
            if(userNames.size() > 0){
                staticsViewInstance.setUserNames(StringUtils.collectionToCommaDelimitedString(userNames));
            }
        }
    }

    @Override
    public int hashCode() {
        return getClass().getSimpleName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return getClass().getSimpleName().equals(obj.getClass().getSimpleName());
    }
}
